var by_orderDetailController = (function () {
    var
        handleOrderBasicItems = function (order) {
            var orderBasicItems = order.orderItems;
            for (var i = 0, length = orderBasicItems.length; i < length; i++) {
                var orderItem = orderBasicItems[i];
                $('#labelDiv').append(by_labelHtml(orderItem.itemName, orderItem.itemValue));                
            }
        },
        handleOrderAttributeItems = function (orderAttributeItems) {
            for (var i = 0, length = orderAttributeItems.length; i < length; i++) {
                var orderAttributeItem = orderAttributeItems[i];
                if (!orderAttributeItem) continue;
                var value = orderAttributeItem.itemValue;
                if (orderAttributeItem.unit) {
                    value = value + '(' + orderAttributeItem.unit + ')';
                }
                if (orderAttributeItem.itemValue) {
                    $('#labelDiv').append(by_labelHtml(orderAttributeItem.itemName, value));
                }
            }
        },
        buildHeaderTitle = function (title, content) {
            $('#spanTitle').append(by_headTitleSpanHtml(title, content));
        },
        buildPhotoItems = function (photoItems) {
            if(!photoItems){
                return;
            }
            for (var i = 0, length = photoItems.length; i < length; i++) {
                var photoItem = photoItems[i];
                if (!photoItem.photoUrls) {
                    continue;
                }
                var photoUrls = photoItem.photoUrls.split(',');
                var showPhotoBtnId = photoItem.photoTypeName + 'show';
                var wrapDivId = photoItem.photoTypeName + 'wrapDivId';
                var appendId = showPhotoBtnId + "appendId";
                $('#photoDiv').append(by_photosHtml.PHOTO_BUTTON_HTML(showPhotoBtnId, photoItem.photoTypeName+ '(共'+photoUrls.length + '张)',appendId));
                $('#' + showPhotoBtnId).click((function (wrapDivId, photoUrls, showPhotoBtnId,photoItem,appendId) {
                    var isHasLoad = false;
                    return function () {
                        if (!isHasLoad) {
                            for (var j = 0, jlength = photoUrls.length; j < jlength; j++) {
                                var photoUrl = by_util.PRE_PHOTO_URL + photoUrls[j];
                                var btnId = photoItem.photoTypeName + i + j;
                                $('#' + appendId).append(by_photosHtml.PHOTO_URL_HTML(wrapDivId, btnId, photoUrl));
                                by_photoEnlargeService(btnId);
                                isHasLoad = true;
                            }
                        }
                        if($("#"+wrapDivId).is(":hidden")){
                            $("#"+wrapDivId).slideToggle("slow");
                            $("#"+showPhotoBtnId).text('↑↑收起'+photoItem.photoTypeName + '(共'+photoUrls.length + '张)');
                        }else{
                            $("#"+wrapDivId).slideToggle("hide");
                            $("#"+showPhotoBtnId).text('↓↓查看'+photoItem.photoTypeName+ '(共'+photoUrls.length + '张)');
                        }
                    }
                })(wrapDivId, photoUrls, showPhotoBtnId, photoItem,appendId));
            }
        },
        buildFileItems = function (fileItems) {
            if(!fileItems){
                return;
            }
            for(var i = 0,length = fileItems.length;i<length;i++){
                var fileItem = fileItems[i];
                var urlSource = by_util.urlSource();
                var fileTypeName = fileItem.fileTypeName;
                if(urlSource!=by_util.FROM_WEB){
                    fileTypeName+='<span style="color:red">(请在电脑上进行查看)<span>';
                }
                $('#photoDiv').append('<label>'+fileTypeName+'</label>');
                var fileUrls = fileItem.fileUrls.split(',');
                for(var j = 0,jlength = fileUrls.length;j<jlength;j++){
                    var fileUrl = fileUrls[j];
                    var fileNameIndex = fileUrl.indexOf('-')+1;
                    var filePre = fileUrl.substring(0,fileNameIndex);
                    var fileName= fileUrl.substring(fileNameIndex,fileUrl.length);
                    $('#photoDiv').append('<br><u>'+fileName+'</u>')
                 
                    if(urlSource==by_util.FROM_WEB){
                        $('#photoDiv').append('<a href="'+by_util.PRE_FILE_URL+filePre+encodeURIComponent(fileName)+'" class="btn btn-primary" style="margin:4px 10px 8px;padding: 3px 6px;">下载</a>');
                    }
                }
            }

        };

    build = function (order) {
        buildHeaderTitle(order.typeDesc, order.statusDesc);
        handleOrderBasicItems(order)
        handleOrderAttributeItems(order.orderAttributeItems);
        buildPhotoItems(order.photoVOs);
        buildFileItems(order.fileVOs);
    }

    return {
        build: build
    }
}($, window));
